/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table.cellRenderer;

import java.awt.Component;
import java.util.Collection;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

import org.jxSwing.i18n.I18N;
import org.jxUtils.TypeUtil;
import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table.cellRenderer
 * @date 28/01/2014 - 19:50:03
 */
public class ButtonRenderer extends JButton implements TableCellRenderer {
	
	private static final long serialVersionUID = 5626012839166372252L;
	
	private static final int DEFAULT_HEIGHT = 50;
	
	private static final int DEFAULT_WIDTH = 80;
	
	private String label = null;
	
	/**
	 * 
	 */
	public ButtonRenderer(final String label) {
	
		this.label = label;
		
		setOpaque(true);
	}
	
	/**
	 * @param value
	 * @param table
	 * @param column
	 * @return label
	 */
	private String getLabel(final Object value, final JTable table, final int column) {
	
		//
		if(value != null) {
			
			//
			if(value instanceof Collection<?>) {
				
				final Collection<?> collection = (Collection<?>)value;
				
				//
				if(collection.isEmpty()) {
					
					return I18N.component().thereAreNoDetails();
					
				}else {
					
					return table.getColumnName(column);
				}
				
			}else if(value instanceof Map<?, ?>) {
				
				final Map<?, ?> map = (Map<?, ?>)value;
				
				//
				if(map.isEmpty()) {
					
					return I18N.component().thereAreNoDetails();
					
				}else {
					
					return table.getColumnName(column);
				}
				
			}else {
				
				final String labelAux = value.toString();
				
				//
				if(labelAux != null && !labelAux.contains(value.getClass().getName() + "@" + Integer.toHexString(value.hashCode()))) {
					
					return labelAux;
				}
			}
			
		}else {
			
			return I18N.component().thereAreNoDetails();
		}
		
		return label;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
	 */
	@Override
	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
	
		//
		if(isSelected) {
			
			setForeground(table.getSelectionForeground());
			setBackground(table.getSelectionBackground());
			
		}else {
			
			setForeground(table.getForeground());
			setBackground(UIManager.getColor("Button.background"));
		}
		
		//
		if(value != null && TypeUtil.isImageIcon(value)) {
			
			final ImageIcon iconValue = (ImageIcon)value;
			
			final int newWidth = iconValue.getIconWidth() > iconValue.getIconHeight() ? DEFAULT_WIDTH : DEFAULT_HEIGHT;
			final int newHeight = newWidth == DEFAULT_WIDTH ? DEFAULT_HEIGHT : DEFAULT_WIDTH;
			
			final ImageIcon icon = ImageUtil.resizeImage(iconValue, newWidth, newHeight);
			
			setText(null);
			setIcon(icon);
			setToolTipText(iconValue.getDescription());
			
			table.setRowHeight(newHeight);
			
		}else {
			
			setText(getLabel(value, table, column));
			setIcon(null);
			setToolTipText(null);
		}
		
		return this;
	}
}
